package com.yiren.dbaa.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tencent.bugly.crashreport.CrashReport;
import com.vividsolutions.jts.geom.LineString;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.bean.device.StationParamConfigBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.backstage.currency.bean.PointBean;
import com.yiren.backstage.currency.bean.PointExBean;
import com.yiren.backstage.currency.entity.CarModelEntity;
import com.yiren.backstage.currency.entity.TrajectoryEntity;
import com.yiren.backstage.currency.entity.TrajectoryPointEntity;
import com.yiren.backstage.currency.utils.GpsUtil;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.constant.G;
import com.yiren.dbaa.module.practice_set.map.api.IJtsJudge;
import com.yiren.dbaa.module.practice_set.map.api.impl.JtsJudgeImpl;
import com.yiren.dbaa.currency.model.MultiLaneModel;
import com.yiren.dbaa.currency.model.TrajectVariableModel;
import com.yiren.dbaa.module.practice_set.map.contract.CollectContract;
import com.yiren.dbaa.module.practice_set.map.presenter.CollectPresenter;
import com.yiren.dbaa.util.AccuracyPixelUtil;
import com.yiren.dbaa.view.interf.IGeometryCreate;
import com.yiren.dbaa.view.interf.ITrajectoryDraw;
import com.yiren.dbaa.view.interf.OnMapViewClickListener;
import com.yiren.dbaa.view.interf.OnMapViewDragListener;
import com.yiren.dbaa.view.interf.impl.GeometryCreateImpl;
import com.yiren.dbaa.view.interf.impl.TrajectoryDrawImpl;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;

/**
 * 类描述：考场轨迹采集视图
 * 创建人：xiaozan
 * 创建时间：2019年7月8日 下午3:09:37
 */
public class TrajectoryMapView extends SurfaceView implements SurfaceHolder.Callback, Runnable {

    private SurfaceHolder holder;
    private Bitmap bitmap;       //车辆模型
    private TrajectVariableModel variableModel;
    private OnMapViewClickListener onMapViewClickListener;

    private CollectContract.Presenter presenter; //高精度考场采集
    private IJtsJudge iJtsJudge;  //高精度评判状态
    private ITrajectoryDraw iTrajectoryDraw;
    private IGeometryCreate iGeometryCreate;

    public HashMap<String, List<TrajectoryPointEntity>> allMap100;  //当前所有轨迹数据
    private long currentTimeMillis = 0;   //当前获取100米范围点的时间

    public List<TrajectoryPointEntity> trajectoryPoints; //点位集合
    public List<MultiLaneModel> laneLists;
    private List<PointExBean> carModelPoints;    //车辆点位集合
    private List<PointBean> carPoints;                   //实时移动车辆点位

    private GpsInfoBean gpsInfo;
    private PointBean currentPoint;     //当前的点位
    private boolean isBaseCalibration;  //点位是否全部用标定的基站采集
    private StationParamConfigBean stationParamConfig;
    public int collectType;             //采集类型

    private Thread thread;
    private boolean running;  //线程是否运行

    public TrajectoryMapView(Context context) {
        this(context, null);
    }

    public TrajectoryMapView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TrajectoryMapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        variableModel = new TrajectVariableModel();
        variableModel.setAccuracy(AccuracyPixelUtil.Accuracy_M7);
        iTrajectoryDraw = new TrajectoryDrawImpl(context);
        iGeometryCreate = new GeometryCreateImpl();
        presenter = new CollectPresenter(context);
        stationParamConfig = LoadConfigApi.loadStationParamConfig(context);
        iJtsJudge = new JtsJudgeImpl();
        currentPoint = new PointBean();
        carPoints = new ArrayList<PointBean>();
        for (int i = 0; i < 33; i++) {
            carPoints.add(new PointBean());
        }
        String carModelId = LoadConfigApi.loadSignalSourceConfig(context).getCarModelId();
        if (StringUtils.isNotEmpty(carModelId)) {
            CarModelEntity carModel = (CarModelEntity) UserDbController.getInstance(false).queryById(carModelId, CarModelEntity.class);
            carModelPoints = new Gson().fromJson(carModel.getPoints(), new TypeToken<List<PointExBean>>() {
            }.getType());
            isBaseCalibration = presenter.loadPointCalibrationStatus();
        }
        getHolder().addCallback(this);
        //差分GPS数据为空时不启动
        thread = new Thread(this);
        running = true;
        thread.start();
    }

    public void setMapViewAttrs(boolean isCollectInterface) {
        if (!isCollectInterface) {
            this.setBackgroundResource(R.color.halfAlpha2);
            this.setZOrderOnTop(true);
            this.getHolder().setFormat(PixelFormat.TRANSLUCENT);
        }
    }

    private int touchX, touchY;
    private int startX, startY;
    private int dealtX, dealtY;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (variableModel.isCollectInterface() || G.isExploitation) {
            int act = event.getAction();
            int x = (int) event.getX();
            int y = (int) event.getY();
            switch (act) {
                case MotionEvent.ACTION_DOWN:
                    touchX = x;
                    touchY = y;
                    startX = x;
                    startY = y;
                    break;
                case MotionEvent.ACTION_MOVE:
                    int dx = x - touchX;
                    int dy = y - touchY;
                    touchX = x;
                    touchY = y;
                    variableModel.setOriginXY(variableModel.getOriginX() + dx, variableModel.getOriginY() + dy);
                    if (!variableModel.isGsdw() && null != gpsInfo) {
                        float distanceX = AccuracyPixelUtil.pixelToDistance(variableModel.getWidth() / 2 - variableModel.getOriginX(), variableModel.getAccuracy());
                        float distanceY = AccuracyPixelUtil.pixelToDistance(variableModel.getOriginY() - variableModel.getHeight() / 2, variableModel.getAccuracy());
                        if (isBaseCalibration && stationParamConfig.isBaseCalibration()) {
                            gpsInfo.setLocation(GpsUtil.getCurLongitude(gpsInfo.getLongitude(), gpsInfo.getLatitude(), AccuracyPixelUtil.pixelToDistance(-dx, variableModel.getAccuracy())), GpsUtil.getCurLatitude(gpsInfo.getLatitude(), AccuracyPixelUtil.pixelToDistance(dy, variableModel.getAccuracy())));
                        } else {
                            gpsInfo.setDistanceXY(distanceX, distanceY);
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    touchX = -1;
                    touchY = -1;
                    dealtX = (int) Math.abs(startX - event.getX());
                    dealtY = (int) Math.abs(startY - event.getY());
                    if (!variableModel.isCollectInterface() && null != onMapViewClickListener && dealtX < 5 && dealtY < 5) {
                        onMapViewClickListener.onMapViewClick();
                    }
                    break;
            }
            return true;
        } else {
            super.onTouchEvent(event);
            return false;
        }
    }

    /**
     * 跟随定位
     */
    private void followLocation() {
        if (gpsInfo != null) {
            float x, y;
            if (isBaseCalibration && stationParamConfig.isBaseCalibration()) {
//                x = distanceToPixel(gpsInfo.getDistanceX());
//                y = distanceToPixel(gpsInfo.getDistanceY());
                String[] locations = stationParamConfig.getBaseStationLocation().split(",");
                x = AccuracyPixelUtil.distanceToPixel(GpsUtil.getDistanceX(Double.parseDouble(locations[0]), Double.parseDouble(locations[1]), gpsInfo.getLongitude()), variableModel.getAccuracy());
                y = AccuracyPixelUtil.distanceToPixel(GpsUtil.getDistanceY(Double.parseDouble(locations[1]), gpsInfo.getLatitude()), variableModel.getAccuracy());
            } else {
                x = AccuracyPixelUtil.distanceToPixel(gpsInfo.getDistanceX(), variableModel.getAccuracy());
                y = AccuracyPixelUtil.distanceToPixel(gpsInfo.getDistanceY(), variableModel.getAccuracy());
            }
            variableModel.setOriginXY((int) (variableModel.getWidth() / 2 - x), (int) (variableModel.getHeight() / 2 + y));
        }
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        this.holder = holder;
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        variableModel.setDimens(width, height);
        variableModel.setOriginXY(width / 2, height / 2);
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        this.holder = holder;
        running = false;
    }

    public void startThread() {
        if (!running) running = true;
    }

    public void stopCheck() {
        running = false;
    }

    public void destroy() {
        running = false;
        thread.interrupt();
    }

    @Override
    public void run() {
        while (running) {
            try {
                if (variableModel.isGsdw()) {
                    gpsInfo = (GpsInfoBean) GpsInfoBean.getInstance().clone();
                    if (G.isExploitation) {
                        if (isBaseCalibration && stationParamConfig.isBaseCalibration()) {
                            float distanceX = -60f;
                            float distanceY = -55f;
                            gpsInfo.setDistanceXY(distanceX, distanceY);
                            String[] baseLocations = stationParamConfig.getBaseStationLocation().split(",");
                            double longitude = Double.parseDouble(baseLocations[0]);
                            double latitude = Double.parseDouble(baseLocations[1]);
                            gpsInfo.setLocation(GpsUtil.getCurLongitude(longitude, latitude, distanceX), GpsUtil.getCurLatitude(latitude, distanceY));
                        } else {
                            gpsInfo.setDistanceXY(-767f, -1416f);
                        }
                        gpsInfo.setAzimuth(0);
                    }
                }
                if (isBaseCalibration && stationParamConfig.isBaseCalibration()) {
                    currentPoint.setLon(gpsInfo.getLongitude());
                    currentPoint.setLat(gpsInfo.getLatitude());
                    String[] baseLocations = stationParamConfig.getBaseStationLocation().split(",");
                    double longitude = Double.parseDouble(baseLocations[0]);
                    double latitude = Double.parseDouble(baseLocations[1]);
                    currentPoint.setX(GpsUtil.getDistanceX(longitude, latitude, gpsInfo.getLongitude()));
                    currentPoint.setY(GpsUtil.getDistanceY(latitude, gpsInfo.getLatitude()));
                } else {
                    currentPoint.setPoint(gpsInfo.getDistanceX(), gpsInfo.getDistanceY());
                }
                followLocation();
                Canvas canvas = null;
                if (null != holder) {
                    canvas = holder.lockCanvas();
                    if (canvas != null) {
                        if (variableModel.isCollectInterface()) {
                            canvas.drawColor(Color.BLACK);
                        } else {
                            canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
                        }
                    }
                    //绘制比例尺
                    iTrajectoryDraw.drawScale(canvas, variableModel);
                    //旋转画布
                    if (!variableModel.isCollect() && canvas != null) {
                        canvas.rotate(-gpsInfo.getAzimuth(), getMeasuredWidth() / 2, getMeasuredHeight() / 2);
                    }
                }
                if (variableModel.isCollect()) {
                    //绘制当前的点
                    iTrajectoryDraw.drawCurrentPoint(canvas, gpsInfo, currentPoint, variableModel);
                    switch (collectType) {
                        case 0:  //多车道
                            drawMultiLane(canvas, 0);
                            break;
                        case 1:  //实线
                        case 7:  //起步靠边实线
                            iTrajectoryDraw.drawCollectSx(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 2:  //虚线
                            iTrajectoryDraw.drawCollectXx(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 3:  //人行横道
                            iTrajectoryDraw.drawCollectRXHD(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 4:  //停止线
                            iTrajectoryDraw.drawCollectTZX(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 5:  //网状线
                        case 8:  //禁停区域
                            iTrajectoryDraw.drawCollectJtqy(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 6:  //中心圈
                            iTrajectoryDraw.drawCollectZxq(canvas, trajectoryPoints, variableModel, carPoints, 0);
                            break;
                        case 9:  //双黄线双实线
                            iTrajectoryDraw.drawCollectShx(canvas, trajectoryPoints, variableModel, 1, 0);
                            break;
                        case 10: //路口直行箭头
                            iTrajectoryDraw.drawCollectLkZX(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 11: //右转标识
                            iTrajectoryDraw.drawCollectLkYZ(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 12: //左转标识
                            iTrajectoryDraw.drawCollectLkZZ(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 13: //掉头标识
                            iTrajectoryDraw.drawCollectLkDT(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 14: //直行左转标识
                            iTrajectoryDraw.drawCollectLkZXZZ(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 15: //直行右转标识
                            iTrajectoryDraw.drawCollectLkZXYZ(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 16: //直行掉头标识
                            iTrajectoryDraw.drawCollectLkZXDT(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 17: //左转掉头标识
                            iTrajectoryDraw.drawCollectLkZZDT(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 18: //直行左转掉头标识
                            iTrajectoryDraw.drawCollectLkZXZZDT(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 19: //双黄线双虚线
                            iTrajectoryDraw.drawCollectShx(canvas, trajectoryPoints, variableModel, 2, 0);
                            break;
                        case 20: //双黄线虚实线
                            iTrajectoryDraw.drawCollectShx(canvas, trajectoryPoints, variableModel, 3, 0);
                            break;
                        case 21: //左右转标识
                            iTrajectoryDraw.drawCollectLkZYZ(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        case 22: //刹车区域
                            iTrajectoryDraw.drawCollectScqy(canvas, trajectoryPoints, variableModel, 0);
                            break;
                        default:
                            break;
                    }
                } else {
                    //更新当前车辆点位
                    iGeometryCreate.updateCarPoints(carPoints, carModelPoints, currentPoint, gpsInfo);
                    if (Math.abs(System.currentTimeMillis() - currentTimeMillis) > 1000) {
                        //1s从数据库读取一次100米范围内的点
                        allMap100 = presenter.loadTrajectory(currentPoint, isBaseCalibration && stationParamConfig.isBaseCalibration());
                        currentTimeMillis = System.currentTimeMillis();
                    }
                    iJtsJudge.checkBackDistance(carPoints);
                    showTrajectory(canvas);
                    drawCar(canvas);
                }
                if (null != holder && canvas != null) {
                    holder.unlockCanvasAndPost(canvas);
                }
                try {
                    Thread.sleep(20);
                } catch (InterruptedException e) {
                    Thread.currentThread().interrupt();  // set interrupt flag
                }
            } catch (Exception e) {
                e.printStackTrace();
                CrashReport.postCatchedException(e);
            }
        }
//        CarStateBean.getInstance().initCarState();
    }

    /**
     * 加载轨迹
     */
    private void showTrajectory(Canvas canvas) {
        if (null == allMap100 || allMap100.size() == 0 || variableModel.isCollect()) {
            //如果附近点位为空   将当前位置变更至距离轨迹最近的坐标    这里只变更一次
            if (variableModel.isCollectInterface() && !variableModel.isGsdw() && !variableModel.isUpLocation()) {
                TrajectoryPointEntity tPoint;
                if (isBaseCalibration && stationParamConfig.isBaseCalibration()) {
                    tPoint = presenter.loadMinTPoint(gpsInfo.getLongitude(), true);
                } else {
                    tPoint = presenter.loadMinTPoint(gpsInfo.getDistanceX());
                }
                if (null != tPoint) {
                    gpsInfo.setLocation(tPoint.getPoint().getLon(), tPoint.getPoint().getLat());
                    gpsInfo.setDistanceXY(tPoint.getPoint().getX(), tPoint.getPoint().getY());
                    variableModel.setUpLocation(true);
                }
            }
            return;
        }
        Iterator<Entry<String, List<TrajectoryPointEntity>>> it = allMap100.entrySet().iterator();
        HashMap<String, List<TrajectoryPointEntity>> shiMap = new HashMap<>();  //实线map
        HashMap<String, List<TrajectoryPointEntity>> xuMap = new HashMap<>();   //虚线map
        HashMap<String, List<TrajectoryPointEntity>> xuAllMap = new HashMap<>();//虚线、虚实线、双虚线map
        HashMap<String, List<TrajectoryPointEntity>> rxhdMap = new HashMap<>(); //人行横道map
        HashMap<String, List<TrajectoryPointEntity>> tzxMap = new HashMap<>();  //停止线map
        HashMap<String, List<TrajectoryPointEntity>> jtqyMap = new HashMap<>(); //禁停区域map
        HashMap<String, List<TrajectoryPointEntity>> zxqMap = new HashMap<>();  //中心圈map
        HashMap<String, List<TrajectoryPointEntity>> qbkbMap = new HashMap<>(); //起步靠边实线map
        HashMap<String, List<TrajectoryPointEntity>> xsjtMap = new HashMap<>(); //行驶箭头标识map
        HashMap<String, List<TrajectoryPointEntity>> scqyMap = new HashMap<>(); //刹车区域map
        HashMap<String, List<TrajectoryPointEntity>> laneMap = new HashMap<>(); //车道map，计算边线距离
        int bmId = 1;   //用来显示轨迹编号
        while (it.hasNext()) {
            Entry m = (Entry) it.next();
            List<TrajectoryPointEntity> points = (List<TrajectoryPointEntity>) m.getValue();
            if (null != points && points.size() > 0 && null != points.get(0).getTrajectory()) {
                switch (points.get(0).getTrajectory().getTrajectoryType()) {
                    case 1:  //实线
                        iTrajectoryDraw.drawCollectSx(canvas, points, variableModel, bmId);
                        shiMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 2:  //虚线
                        iTrajectoryDraw.drawCollectXx(canvas, points, variableModel, bmId);
                        xuMap.put(m.getKey().toString(), points);
                        xuAllMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 3:  //人形横道
                        iTrajectoryDraw.drawCollectRXHD(canvas, points, variableModel, bmId);
                        rxhdMap.put(m.getKey().toString(), points);
                        jtqyMap.put(m.getKey().toString(), points);
                        break;
                    case 4:  //停止线
                        iTrajectoryDraw.drawCollectTZX(canvas, points, variableModel, bmId);
                        tzxMap.put(m.getKey().toString(), points);
                        jtqyMap.put(m.getKey().toString(), points);
                        break;
                    case 5:  //网状线
                    case 8:  //禁停区域
                        iTrajectoryDraw.drawCollectJtqy(canvas, points, variableModel, bmId);
                        jtqyMap.put(m.getKey().toString(), points);
                        break;
                    case 6:  //中心圈
                        iTrajectoryDraw.drawCollectZxq(canvas, points, variableModel, carPoints, bmId);
                        zxqMap.put(m.getKey().toString(), points);
                        break;
                    case 7:  //起步靠边实线
                        iTrajectoryDraw.drawCollectSx(canvas, points, variableModel, bmId);
                        qbkbMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 9:  //双黄线
                        iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 1, bmId);
                        shiMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 10: //路口直行箭头
                        iTrajectoryDraw.drawCollectLkZX(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 11: //右转标识
                        iTrajectoryDraw.drawCollectLkYZ(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 12: //左转标识
                        iTrajectoryDraw.drawCollectLkZZ(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 13: //掉头标识
                        iTrajectoryDraw.drawCollectLkDT(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 14: //直行左转标识
                        iTrajectoryDraw.drawCollectLkZXZZ(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 15: //直行右转标识
                        iTrajectoryDraw.drawCollectLkZXYZ(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 16: //直行掉头标识
                        iTrajectoryDraw.drawCollectLkZXDT(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 17: //左转掉头标识
                        iTrajectoryDraw.drawCollectLkZZDT(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 18: //直行左转掉头标识
                        iTrajectoryDraw.drawCollectLkZXZZDT(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 19: //双虚线
                        iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 2, bmId);
                        xuAllMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 20: //虚实线
                        iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 3, bmId);
                        shiMap.put(m.getKey().toString(), points);
                        xuAllMap.put(m.getKey().toString(), points);
                        laneMap.put(m.getKey().toString(), points);
                        break;
                    case 21: //路口左右转
                        iTrajectoryDraw.drawCollectLkZYZ(canvas, points, variableModel, bmId);
                        xsjtMap.put(m.getKey().toString(), points);
                        break;
                    case 22: //刹车区域
                        if (points.size() == 4) {
                            iTrajectoryDraw.drawCollectScqy(canvas, points, variableModel, bmId);
                            scqyMap.put(m.getKey().toString(), points);
                        }
                        break;
                    default:
                        break;
                }
            }
            bmId++;
        }
        iJtsJudge.checkCarPressSLine(shiMap, variableModel);
        iJtsJudge.checkCarPressXLine(xuAllMap, variableModel);
        iJtsJudge.checkRxhdRelative(rxhdMap, variableModel);
        iJtsJudge.checkCarPassTzx(tzxMap, variableModel);
        iJtsJudge.checkCarStopJtqy(jtqyMap, variableModel);
        iJtsJudge.checkCarPressZxq(zxqMap, variableModel);
        iJtsJudge.checkCarPressQbkb(qbkbMap, variableModel);
        iJtsJudge.checkCarPressXsjt(xsjtMap, variableModel);
        iJtsJudge.checkCarIntersectScqy(scqyMap, variableModel);
        iJtsJudge.checkCarChangeLane(xuMap, carPoints, variableModel);
        iJtsJudge.checkCarLaneDistance(laneMap, carPoints, variableModel);
    }

    private void drawCar(Canvas canvas) {
        LineString[] LineStrings = iGeometryCreate.getCarLineString(carPoints);
        variableModel.setCarLineString(LineStrings[0]);
        variableModel.setWheelLineString(LineStrings[1]);
        //绘制车辆
        iTrajectoryDraw.drawCar(canvas, carPoints, bitmap, gpsInfo.getAzimuth(), variableModel);
    }

    /**
     * 绘制多车道
     */
    private void drawMultiLane(Canvas canvas, int bmId) {
        if (null != laneLists && laneLists.size() > 0) {
            for (int i = 0; i < laneLists.size(); i++) {
                List<TrajectoryPointEntity> points = laneLists.get(i).getTrajectoryPoints();
                if (null != points && points.size() > 0) {
                    int trajectoryType = points.get(0).getTrajectory().getTrajectoryType();
                    switch (trajectoryType) {
                        case 1:
                        case 7:
                            iTrajectoryDraw.drawCollectSx(canvas, points, variableModel, bmId);
                            break;
                        case 2:
                            iTrajectoryDraw.drawCollectXx(canvas, points, variableModel, bmId);
                            break;
                        case 9:
                            iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 1, bmId);
                            break;
                        case 19:
                            iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 2, bmId);
                            break;
                        case 20:
                            iTrajectoryDraw.drawCollectShx(canvas, points, variableModel, 3, bmId);
                            break;
                    }
                }
            }
        }
    }

    /**
     * 地图比率缩放
     */
    public void setAccuracy(boolean up) {
        float x = AccuracyPixelUtil.pixelToDistance(variableModel.getWidth() / 2 - variableModel.getOriginX(), variableModel.getAccuracy());
        float y = AccuracyPixelUtil.pixelToDistance(variableModel.getOriginY() - variableModel.getHeight() / 2, variableModel.getAccuracy());
        int accuracy = up ? variableModel.getAccuracy() + 1 : variableModel.getAccuracy() - 1;
        if (accuracy < AccuracyPixelUtil.Accuracy_M2) accuracy = AccuracyPixelUtil.Accuracy_M2;
        if (accuracy > AccuracyPixelUtil.Accuracy_M30)
            accuracy = AccuracyPixelUtil.Accuracy_M30;
        variableModel.setAccuracy(accuracy);
        variableModel.setOriginXY(variableModel.getWidth() / 2 - AccuracyPixelUtil.distanceToPixel(x, accuracy), variableModel.getHeight() / 2 + AccuracyPixelUtil.distanceToPixel(y, accuracy));
    }

    public void setBitmap(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public void setAccuracy(int accuracy) {
        variableModel.setAccuracy(accuracy);
    }

    public void setCollectInterface(boolean collectInterface) {
        variableModel.setCollectInterface(collectInterface);
    }

    public void setCollect(boolean collect) {
        variableModel.setCollect(collect);
    }

    public boolean isCollect() {
        return variableModel.isCollect();
    }

    public void setShowBsPoint(boolean showBsPoint) {
        variableModel.setShowBsPoint(showBsPoint);
    }

    public void setGsdw(boolean gsdw) {
        variableModel.setGsdw(gsdw);
    }

    public void setUpLocation(boolean upLocation) {
        variableModel.setUpLocation(upLocation);
    }

    public void setCurrentUpdateTrajectory(TrajectoryEntity currentUpdateTrajectory) {
        variableModel.setCurrentUpdateTrajectory(currentUpdateTrajectory);
    }

    public TrajectoryEntity getCurrentUpdateTrajectory() {
        return variableModel.getCurrentUpdateTrajectory();
    }

    public void setCurrentUpdatePoint(TrajectoryPointEntity currentUpdatePoint) {
        variableModel.setCurrentUpdatePoint(currentUpdatePoint);
    }

    public TrajectoryPointEntity getCurrentUpdatePoint() {
        return variableModel.getCurrentUpdatePoint();
    }

    public void setOnMapViewClickListener(OnMapViewClickListener onMapViewClickListener) {
        this.onMapViewClickListener = onMapViewClickListener;
    }

    public void setOnMapViewDragListener(OnMapViewDragListener onMapViewDragListener) {
        variableModel.setOnMapViewDragListener(onMapViewDragListener);
    }

}
